package com.yxhl.zoume.utils;

import android.content.Context;
import android.support.annotation.NonNull;

import com.afollestad.materialdialogs.DialogAction;
import com.afollestad.materialdialogs.MaterialDialog;
import com.yxhl.zoume.domain.interactor.ZMEventSubscriber;

/**
 * Created by vhawk on 16/5/23.
 */
public class ZMDialog {


    private final MaterialDialog materialDialog;

    private ZMDialog(final Builder zmDialogBuilder) {
        MaterialDialog.Builder materialBuilder = new MaterialDialog.Builder(zmDialogBuilder.getContext());
        materialDialog = materialBuilder.canceledOnTouchOutside(zmDialogBuilder.isCancelable())
                .title(zmDialogBuilder.getTitle())
                .content(zmDialogBuilder.getMessage())
                .positiveText(zmDialogBuilder.getPositiveMessage())
                .negativeText(zmDialogBuilder.getNegativeMessage())
                .neutralText(zmDialogBuilder.getNeutralMessage())
                .onPositive(new MaterialDialog.SingleButtonCallback() {
                    @Override
                    public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                        zmDialogBuilder.getPositiveClickEvent().onNext("");
                    }
                })
                .onNeutral(new MaterialDialog.SingleButtonCallback() {
                    @Override
                    public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                        if (zmDialogBuilder.getNeutralClickEvent() != null) {
                            zmDialogBuilder.getNeutralClickEvent().onNext("");
                        }
                    }
                })
                .onNegative(new MaterialDialog.SingleButtonCallback() {
                    @Override
                    public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                        if (zmDialogBuilder.getNegativeClickEvent() != null) {
                            zmDialogBuilder.getNegativeClickEvent().onNext("");
                        }
                    }
                })
                .canceledOnTouchOutside(false)
                .build();
        materialDialog.show();
    }


    /**
     * @param zmDialogBuilder
     * @param mode
     */
    private ZMDialog(final Builder zmDialogBuilder, int mode) {
        MaterialDialog.Builder materialBuilder = new MaterialDialog.Builder(zmDialogBuilder.getContext());
        materialDialog = materialBuilder.canceledOnTouchOutside(zmDialogBuilder.isCancelable())
                .title(zmDialogBuilder.getTitle())
                .content(zmDialogBuilder.getMessage())
                .positiveText(zmDialogBuilder.getPositiveMessage())
                .neutralText(zmDialogBuilder.getNeutralMessage())
                .negativeText(zmDialogBuilder.getNegativeMessage())
                .onPositive(new MaterialDialog.SingleButtonCallback() {
                    @Override
                    public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                        zmDialogBuilder.getPositiveClickEvent().onNext("");
                    }
                })
                .onNeutral(new MaterialDialog.SingleButtonCallback(){
                    @Override
                    public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                        zmDialogBuilder.getNeutralClickEvent().onNext("");
                    }
                })
                .onNegative(new MaterialDialog.SingleButtonCallback() {
                    @Override
                    public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                        if (zmDialogBuilder.getNegativeClickEvent() != null) {
                            zmDialogBuilder.getNegativeClickEvent().onNext("");
                        }
                    }
                })
                .build();
    }

    public boolean isShowing() {
        return materialDialog.isShowing();
    }

    public static class Builder {

        private Context context;
        private String title;
        private CharSequence message;
        private String positiveMessage;
        private String negativeMessage;
        private String neutralMessage;
        private ZMEventSubscriber positiveClickEvent;
        private ZMEventSubscriber negativeClickEvent;
        private ZMEventSubscriber neutralClickEvent;
        private boolean cancelable;

        public Builder(Context context) {
            this.context = context;
        }

        public Context getContext() {
            return context;
        }


        public String getTitle() {
            return title;
        }

        public Builder setTitle(String title) {
            this.title = title;
            return this;
        }

        public CharSequence getMessage() {
            return message;
        }

        public Builder setMessage(CharSequence message) {
            this.message = message;
            return this;
        }

        public String getPositiveMessage() {
            return positiveMessage;
        }

        public Builder setPositiveMessage(String positiveMessage) {
            this.positiveMessage = positiveMessage;
            return this;
        }

        public String getNegativeMessage() {
            return negativeMessage;
        }

        /**
         * 单选对话框的话，需要用这个
         *
         * @param negativeMessage
         * @return
         */
        public Builder setNegativeMessage(String negativeMessage) {
            this.negativeMessage = negativeMessage;
            return this;
        }

        public ZMEventSubscriber getPositiveClickEvent() {
            return positiveClickEvent;
        }


        public String getNeutralMessage() {
            return neutralMessage;
        }

        public Builder setNeutralMessage(String neutralMessage) {
            this.neutralMessage = neutralMessage;
            return this;
        }

        public ZMEventSubscriber getNeutralClickEvent() {
            return neutralClickEvent;
        }

        public Builder setNeutralClickEvent(ZMEventSubscriber neutralClickEvent) {
            this.neutralClickEvent = neutralClickEvent;
            return this;
        }

        public Builder setPositiveClickEvent(ZMEventSubscriber positiveClickEvent) {
            this.positiveClickEvent = positiveClickEvent;
            return this;
        }

        public ZMEventSubscriber getNegativeClickEvent() {
            return negativeClickEvent;
        }

        /**
         * 单选对话框的话，需要用这个
         *
         * @param negativeClickEvent
         * @return
         */
        public Builder setNegativeClickEvent(ZMEventSubscriber negativeClickEvent) {
            this.negativeClickEvent = negativeClickEvent;
            return this;
        }

        public boolean isCancelable() {
            return cancelable;
        }

        public Builder setCancelable(boolean cancelable) {
            this.cancelable = cancelable;
            return this;
        }

        public ZMDialog show() {
            return new ZMDialog(this);
        }

        public MaterialDialog builder() {
            return new ZMDialog(this, 0).build();
        }
    }

    private MaterialDialog build() {
        return materialDialog;
    }

    public static MaterialDialog getLoadingDialog(Context context) {
        MaterialDialog.Builder builder = new MaterialDialog.Builder(context);
        builder.content("正在处理...")
                .progress(true, 0);

        return builder.build();
    }
}
